//
//  LuxuryViewController.m
//  DuBai
//
//  Created by darktm on 14-2-23.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "LuxuryCategoryViewController.h"

#import "TaskWithGetVersionUpdate.h"

#import "LuxuryCategoryCollectonCell.h"
#import "UIImageView+WebCache.h"

#import "CommodityDetailViewController.h"
#import "LuxuryFiltrateResultViewController.h"

#import "Brand.h"
#import "DBManager.h"
#import "TaskWithGetVersionUpdate.h"

@interface LuxuryCategoryViewController ()<WYHttpTaskDelegate>

@property(nonatomic,strong)NSMutableArray *brandArray;

@end

@implementation LuxuryCategoryViewController

@synthesize brandArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.btn.layer.cornerRadius = 13.0f;
    
    [self.cv registerClass:[LuxuryCategoryCollectonCell class] forCellWithReuseIdentifier:@"my_cell"];

    brandArray = [NSMutableArray array];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on_keyboard_will_show:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on_keyboard_will_hiden:) name:UIKeyboardWillHideNotification object:nil];
    
    [self loadBrandWithName:nil];
}

-(void)loadBrandWithName:(NSString*)name{
    NSDictionary *d;
    
    if(name.length && name){
        d = [NSDictionary dictionaryWithObject:name forKey:@"name"];
    }
    NSArray *a = [[[DBManager shareInstance:DBTypeCn] db] queryObjWithClass:[Brand class] condition:d];
    [brandArray addObjectsFromArray:a];
    [self.cv reloadData];
}


#pragma mark - UICollectionView delegate -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    int count = ceilf(brandArray.count / 2.0f);
    
    if(section == count - 1)
        return brandArray.count - 2 * section;
    
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LuxuryCategoryCollectonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BrandCollectonCell" forIndexPath:indexPath];
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [[[UIColor brownColor] colorWithAlphaComponent:.2] CGColor];

    int index = indexPath.section * 2 + indexPath.item;

    Brand *o = brandArray[index];
    cell.name.text = o.name;
    [cell.image setImageWithURL:[NSURL URLWithString:o.desc]];

    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    int count = ceilf(brandArray.count / 2.0f);
    return count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    int index = indexPath.section * 2 + indexPath.item;
    
    DLog(@"%@,",indexPath);
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if([segue.identifier isEqualToString:@"LuxuryFiltrateResult"]){//商品详情页面
        //赋值
        LuxuryFiltrateResultViewController *rsut = segue.destinationViewController;
        
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
 
    [self.txt resignFirstResponder];
    
    if(self.prompt){
        [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            self.prompt.alpha = 0;
        } completion:^(BOOL finished) {
            [self.prompt removeFromSuperview];
            self.prompt = nil;
        }];
    }
}
#pragma mark - notification -

-(void)on_keyboard_will_show:(NSNotification *)n{
    self.prompt = [[UIView alloc] initWithFrame:CGRectMake(0, 44, kWidth, kHeight - 64)];
    self.prompt.backgroundColor = [UIColor darkGrayColor];
    self.prompt.alpha = 0;
    [self.view addSubview:self.prompt];
    
    
    [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.prompt.alpha = .5;
    } completion:NULL];
}
-(void)on_keyboard_will_hiden:(NSNotification *)n{
    [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.prompt.alpha = 0;
    } completion:^(BOOL finished) {
        [self.prompt removeFromSuperview];
        self.prompt = nil;
    }];
}


#pragma mark - request delegate -
-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];
    if([obj isKindOfClass:[TaskWithGetVersionUpdate class]]){
        [brandArray addObjectsFromArray:rsult];
        [self.cv reloadData];
    }
    
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
}


#pragma mark - btn action -

- (IBAction)on_btn_search:(UIButton *)sender {
    
    if(!self.txt.text || !self.txt.text.length){
        [UIAlertView showAlertViewWithTitle:@"请输入关键字"];
        return;
    }
    
    [self.txt resignFirstResponder];
    
    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LuxuryFiltrateResultViewController * vc = [s instantiateViewControllerWithIdentifier:@"LuxuryFiltrateResultViewController"];
    vc.keyword = self.txt.text;
    [self.navigationController pushViewController:vc animated:YES];
    
    self.txt.text = @"";
}
@end
